package com.lagouedu.order.config;

import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.TransactionMQProducer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RocketMqConfig {

    @Bean
    /**
     * 事务消息生产者
     */
    public TransactionMQProducer getTransactionMQProducer() throws MQClientException {
        TransactionMQProducer transactionMQProducer = new TransactionMQProducer("tx_producer_grp_01");
        transactionMQProducer.setNamesrvAddr("localhost:9876");
//        TransactionListener transactionListener = new TransactionListener() {
//            @Override
//            /**
//             * 本地事务执行 若执行失败 删除HALF MESSAGE
//             */
//            public LocalTransactionState executeLocalTransaction(Message message, Object o) {
//                // 执行本地事务
//
//                return null;
//            }
//
//            @Override
//            public LocalTransactionState checkLocalTransaction(MessageExt messageExt) {
//                return null;
//            }
//        };
        // 设置消息发送失败重试
        transactionMQProducer.setRetryTimesWhenSendFailed(3); //失败的情况重发3次
        transactionMQProducer.start();
        return transactionMQProducer;
    }
}
